home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Processes / MP Threaded Sort / Sort / SortPicts2.cp < prev    next >
Encoding:
Text File  |  1997-03-13  |  4.7 KB  |  253 lines  |  [TEXT/CWIE]

  1. #include "SortPicts.h"
  2. #include <TextUtils.h>
  3. #include <Resources.h>
  4. #include <string.h>
  5.  
  6. extern long            gNumSortPictResources;
  7. extern MenuHandle    gPicturesMenu;
  8. extern Boolean        gUseCoplandTasks;
  9.  
  10. Boolean    SortPicts::PrepareGWorld( ConstStr255Param pictName)
  11. {
  12.  
  13.     if( !LoadSortPicture( pictName))
  14.         return false;
  15.  
  16.     if( !MakeSortGWorld())
  17.     {
  18.         ReleaseSortPicture();
  19.         return false;
  20.     }
  21.  
  22.     ReleaseSortPicture();
  23.  
  24.     if( !AllocSortData())
  25.     {
  26.         DisposeGWorld( sortGWorld);
  27.         return false;
  28.     }
  29.     
  30.     MakeSortData();
  31.     InitializeSortWindowRect( pictName);
  32.     
  33.     return true;            //    YES!! The everything worked out!
  34. }
  35.  
  36. Boolean    SortPicts::LoadSortPicture( ConstStr255Param pictName)
  37. {
  38.     sortPict = (PicHandle)GetNamedResource( 'PICT', pictName);
  39.     
  40.     if( sortPict == nil)
  41.         return false;
  42.     
  43.     sortRect = (*sortPict)->picFrame;
  44.     pictWidth = sortRect.right - sortRect.left;
  45.     pictHeight = sortRect.bottom - sortRect.top;
  46.     
  47.     SetRect(   &sortRect, 0, 0, (short) pictWidth, (short) pictHeight);
  48.     copyBitsRect = sortRect;
  49.     
  50.     return true;
  51. }
  52.  
  53. void    SortPicts::ReleaseSortPicture( void)
  54. {
  55.     ReleaseResource( (Handle) sortPict);
  56.  
  57.     sortPict = nil;
  58. }
  59.  
  60. Boolean    SortPicts::MakeSortGWorld( void)
  61. {
  62.     OSErr            myOSErr;
  63.     GWorldPtr        tempSortGWorld;
  64.     GDHandle        oldGDevice;
  65.     CGrafPtr        oldGWorld;
  66.     Rect            tempSortRect;
  67.  
  68.     tempSortRect = sortRect;
  69.  
  70.     GetGWorld( &oldGWorld, &oldGDevice);
  71.     
  72.     myOSErr = NewGWorld( &tempSortGWorld, 8, &tempSortRect, 
  73.                         (CTabHandle) 0, (GDHandle) 0, keepLocal);
  74.     
  75.     if( myOSErr != noErr)
  76.         return false;
  77.             
  78.     sortGWorld = tempSortGWorld;
  79.     
  80.     SetGWorld( sortGWorld, (GDHandle)0);
  81.     sortPixmap = GetGWorldPixMap( sortGWorld );
  82.     LockPixels( sortPixmap);
  83.  
  84.     
  85.     EraseRect( &sortRect);
  86.     DrawPicture( sortPict, &sortRect);
  87.     
  88.     UnlockPixels( sortPixmap );
  89.     SetGWorld( oldGWorld, oldGDevice);
  90.  
  91.     return true;    
  92. }
  93.  
  94.  
  95. Boolean    SortPicts::AllocSortData( void)
  96. {
  97.     sortPixmap = GetGWorldPixMap( sortGWorld);
  98.  
  99.     pictRowBytes = (*sortPixmap)->rowBytes & 0x3FFF;
  100.  
  101.     N = pictWidth * pictHeight;
  102.     
  103.     sortHandle = (SortDataHandle) NewHandle( N * sizeof(long) );
  104.                 
  105.     if( sortHandle == nil)
  106.         return false;
  107.     
  108.     if ( gUseCoplandTasks )
  109.     {
  110.         HLockHi( (Handle)sortHandle );
  111.         sortData = *sortHandle;
  112.         
  113.         // • Create a pixel buffer for the copland task.  It doesn't really have
  114.         // • access to the memory that's in the GWorld...
  115.         //pixelBuffer =  NewHandle( N );
  116.         //HLockHi( (Handle)pixelBuffer );
  117.         //pixelBufferData = *pixelBuffer;
  118.     }
  119.  
  120.     return true;
  121. }
  122.  
  123.  
  124. void    SortPicts::CopyFromSortData( void )
  125. {
  126.     long            horiz;
  127.     long            loop;
  128.     SortPixel        pixel;
  129.     
  130.     horiz = 0;
  131.  
  132.     UseSortData();
  133.  
  134.     for( loop = 0; loop < N; ++loop)
  135.     {
  136.         pixel = sortData[loop];
  137.         sortPixels[horiz] = pixel;
  138.         //pixelBufferData[loop] = pixel;
  139.         
  140.         if( ++horiz == pictWidth)
  141.         {
  142.             horiz = 0;
  143.             sortPixels += pictRowBytes;
  144.         }
  145.     }
  146.     
  147.     UnuseSortData();    
  148. }
  149.  
  150.  
  151. void    SortPicts::ReleaseSortData( void)
  152. {
  153.     DisposeHandle( (Handle) sortHandle );
  154.     sortHandle = nil;
  155.     
  156.     //DisposeHandle( (Handle) pixelBuffer );
  157.     //pixelBuffer = nil;
  158.     //pixelBufferData = nil;
  159. }
  160.  
  161.  
  162. void    SortPicts::MakeSortData( void)
  163. {
  164.     long            horiz;
  165.     long            loop;
  166.     SortPixel        pixel;
  167.     
  168.     UseSortData();
  169.  
  170.     horiz = 0;
  171.  
  172.     for( loop = 0; loop < N; ++loop)
  173.     {
  174.         pixel = sortPixels[horiz];
  175.         sortData[loop] = ((SortData) pixel) | (loop << 8);
  176.         //pixelBufferData[loop] = pixel;
  177.         
  178.         if( ++horiz == pictWidth)
  179.         {
  180.             horiz = 0;
  181.             sortPixels += pictRowBytes;
  182.         }
  183.     }
  184.  
  185.     UnuseSortData();    
  186. }
  187.  
  188.  
  189. void    SortPicts::InitializeSortWindowRect( ConstStr255Param pictName)
  190. {
  191.     Rect            **windPictRectResource;
  192.     
  193.     gNumWindowsCreated++;
  194.     
  195.     if( (gPrefsRef > 0) &&
  196.         ((windPictRectResource = (Rect **) GetNamedResource( 'RECT', pictName)) != NULL))
  197.     {
  198.             windPictRect = **windPictRectResource;
  199.     }
  200.     else
  201.     {
  202.         windPictRect = sortRect;
  203.         windPictRect.top += kWindPictRectVoffset;
  204.         windPictRect.bottom += kWindPictRectVoffset;
  205.         
  206.         OffsetRect( &windPictRect,
  207.                     (short) gNumWindowsCreated * 10, (short) gNumWindowsCreated * 10 + 30);
  208.     }
  209. }
  210.  
  211. void    SortPicts::OpenDefaultSortWindows( void)
  212. {
  213.     SortPicts    *newWindow;
  214.     Str255        defaultWindowName;
  215.     short        i;
  216.     
  217.     /*    Open additional windows for each of the Preferences Rects out there    */
  218.     i = 2;
  219.     do
  220.     {
  221.         GetIndString( defaultWindowName, kDefaultWindowResource, i);
  222.         if( defaultWindowName[0] == 0)
  223.             break;
  224.         
  225.         newWindow = new SortPicts( defaultWindowName);
  226.         
  227.         i ++;
  228.     } while( true);
  229.     
  230.     GetIndString( defaultWindowName, kDefaultWindowResource, 1);
  231.     
  232.     if( defaultWindowName[0] != 0)
  233.     {
  234.         ::strcpy( (char *) sortPictsName, (char *) defaultWindowName);
  235.         CreateWindow( kNormalWindow);
  236.     }
  237.     else
  238.     {
  239.         SelectRandomPicture();    
  240.         CreateWindow( kNormalWindow);
  241.     }
  242. }
  243.  
  244. void    SortPicts::SelectRandomPicture( void)
  245. {
  246.     long        r;
  247.     Str255        randomPictName;
  248.     
  249.     r = Random( gNumSortPictResources);
  250.     GetMenuItemText( gPicturesMenu, (short) r, randomPictName);
  251.     ::strcpy( (char *) sortPictsName, (char *) randomPictName);
  252. }
  253.